using UnityEngine;
using System.Collections;

public class Mouvement : MonoBehaviour
{
		public float speed = 3;
		private bool block_up = false;
		private bool block_left = false;
		private bool block_right = false;
		private bool block_down = false;
		private bool jumpDelay = false;
		private int doubleJump = 0;


		// Use this for initialization
		void Start ()
		{

		}

		void Update ()
		{
				if (main.gameIsRunning) {

						if (Input.GetKeyDown (KeyCode.Space) && jumpDelay == false) {
								Jump ();                    
						}
						if (Input.GetKey (KeyCode.RightArrow) && !this.block_right) {
								transform.position += new Vector3 (speed * Time.deltaTime, 0, 0);
						}
						if (Input.GetKey (KeyCode.LeftArrow) && !this.block_left) {
								transform.position -= new Vector3 (speed * Time.deltaTime, 0, 0);
						}
						if (Input.GetKey (KeyCode.DownArrow) && !this.block_down) {
								gameObject.rigidbody2D.velocity = new Vector2 (0, -7);
						}
						if (Input.GetKey (KeyCode.UpArrow) && !this.block_up) {
								transform.position += new Vector3 (0, speed * Time.deltaTime, 0);
						}
				} else {
						if (Input.GetKeyDown (KeyCode.Space) && jumpDelay == false) {
								main.gameIsRunning = true;   
								GameObject.Find ("Sphere").rigidbody2D.isKinematic = false;
						}
				}
		}

		public void Jump ()
		{
				if (doubleJump <= 1) {
						gameObject.rigidbody2D.velocity = new Vector2 (0, 6);
						jumpTimer ();
				}
		}
	
		public void jumpTimer ()
		{
				if (Input.GetKeyDown (KeyCode.Space)) {
						doubleJump ++;
				}
		
				if (doubleJump > 1) {
						doubleJump = 0;
						jumpDelay = true;
						jumpDelay = false;
				}
		}
}
